package com.xxl.springcloud.controller;

import com.xxl.springcloud.entities.CommonResult;
import com.xxl.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
public class OrderController {
    public static final String PAYMENT_URL = "http://localhost:8001";

    @Autowired
    private RestTemplate restTemplate;

    // 远程调用支付接口
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        /*
        *   使用restTemplate访问restful接口非常的简单粗暴无脑。
            (url, requestMap, ResponseBean.class)这三个参数分别代表。
            REST请求地址、请求参数、HTTP响应转换被转换成的对象类型
        *
        * */
        return  restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }


    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}
